<template>
	<view class="content">
		 <u-navbar :is-back="true" title="设置群公告" title-color="#303233" :title-size="34">
			 <view slot="right" class="u-p-r-30" v-if="memberType>1">
				 <u-button @click="updateGroupNotice" size="mini"  type="success"  
				  :disabled="groupNotice.length<1&&advShowFlag">完成</u-button>
			 </view>
		 </u-navbar>
		 <view class="u-p-30">
			 <u-form-item label="公告显示" :label-style="{color:'#A5A5A5'}" :label-width="150"  v-if="memberType>1">
				 <u-switch active-color="#19BE6B" slot="right" v-model="advShowFlag" @change="changeAdvFlag"></u-switch>
			</u-form-item>
			 
			 <u-form-item label="群公告:" :label-width="150" label-position="top" :label-style="{color:'#A5A5A5'}" 
				:border-bottom="false">
				  <u-input v-model="groupNotice"  
					type="textarea" auto-height :height="200" 
					:disabled="memberType<1"
					:custom-style="{backgroundColor:'#F2F2F7',borderRadius: '10rpx',padding:'10rpx 30rpx',fontSize:'32rpx'}"
				    :maxlength="maxLength" :clearable="true" :focus="memberType>1" :trim="true"
				    placeholder="请输入群公告内容" />
			 </u-form-item>
		 </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				groupId:"",
				groupNotice:"",
				maxLength:-1,
				titleStyle:{
					fontSize:"32rpx",
					color:"#333333",
				},
				advShowFlag:true,
				memberType:1,
			};
		},
		onLoad:function(e){
			console.log("option====",e);
			this.groupId=e.groupId;
			if(e.groupNotice){
				this.groupNotice=e.groupNotice;
			}
			if(e.advShowFlag){
				if(e.advShowFlag=="true"){
					this.advShowFlag=true;
				}
				else{
					this.advShowFlag=false;
				}
			}
			if(e.memberType){
				this.memberType=Number(e.memberType);
			}
		},
		onBackPress:function(event){
			console.log("监听修改群名称页面回退");
			//this.$store.commit('setPageRefresh',false);
			// #ifndef H5
			 plus.key.hideSoftKeybord();
			// #endif
			uni.hideKeyboard();
		},
		
		methods:{
			onInput:function(val){
				let that=this;
				if(val.length>=16){
					uni.hideKeyboard();
					// #ifndef H5
					  plus.key.hideSoftKeybord();
					// #endif
					this.globalUtil.utilAlert("抱歉,不能超过16个字!");
					return;
				}
			},
			
			//是否显示群公告
			changeAdvFlag:function(advFlag){
				let that=this;
				let hiddenFlag=0;
				if(advFlag==true){
					this.showNoticeFlag=1;
				}else{
					this.showNoticeFlag=0;
				}
			},
			
			updateGroupNotice:function(){
				let that=this;
				uni.hideKeyboard();
				// #ifndef H5
				  plus.key.hideSoftKeybord();
				// #endif
				if(that.groupNotice.length<1&&this.showNoticeFlag==1){
					this.globalUtil.utilAlert("内容不能为空!","warning");
					return;
				}
				let param={
					"groupId":this.groupId,
					"groupNotice":this.groupNotice,
					"showNoticeFlag":this.showNoticeFlag
				};
				that.localGroupApi.updateGroupInfoFromServer(that,param,(result)=>{
					console.log("修改群聊名称结果",result);
					if(result==true){
						// #ifndef H5
						plus.key.hideSoftKeybord();
						// #endif
						uni.hideKeyboard();
						let timestamp = new Date().getTime();
						let message = {
							"id": new Date().getTime() + "",
							"chatGroupId": that.groupId,
							"userId": that.vuex_userInfo.id,
							"meFlag": true,
							"userName": that.vuex_userInfo.userName,
							"userAvatar": that.vuex_userInfo.avatar ? that.vuex_userInfo.avatar :
								"/static/image/default/default-user/default-user.png",
							'messageType': that.messageApi.MSG_TYPE.SYSTEM_MSG,
							"contentType":  that.messageApi.CONTENT_TYPE.TEXT_CONTENT_TYPE,
							"content": {
								'text':this.showNoticeFlag==1?"管理员更新了群公告":'管理员关闭了群公告',
								'sysMsgType':that.messageApi.SYS_MSG_TYPE.updateGroupNotice,
								'param':param,
							},
							"createTime": that.globalUtil.getTimeStr(timestamp),
							"timestamp": timestamp,
							"platFrom": 1,
							"showTimeFlag": true,
						}
						console.log("发送修改群公告的系统信息", message);
						this.globalWX.sendMessage(message, {
							complete: function(res) {
								if (res == true) {
									uni.$emit("groupInfoListener",param);
								} else {
									that.globalUtil.utilAlert("网络故障，稍后重试！");
									return;
								}
							}
						},that.messageApi.BUSINESS_CODE.chatting);
						uni.navigateBack({
							delta:1,
							animationType:'pop-out',
							animationDuration:200
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
   .right{
	   margin: 0px 10upx;
   }
</style>
